package android07.newtop.presenter;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;

import com.jude.beam.bijection.Presenter;

import android07.newtop.adapter.WeatherListAdapter;
import android07.newtop.fragment.WeatherFragment;
import android07.newtop.model.WeatherBeanModel;
import android07.newtop.model.bean.WeatherInfoBean;
import android07.newtop.utils.JUtils;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class WeatherFragmentPresenter extends Presenter<WeatherFragment> implements SwipeRefreshLayout.OnRefreshListener {

    private static final String TAG = WeatherFragmentPresenter.class.getSimpleName();
    private WeatherListAdapter adapter;

    private String tab;

    @Override
    protected void onCreateView(WeatherFragment view) {
        super.onCreateView(view);
//        tab = view.getResources().getStringArray(R.array.tab)[view.getArguments().getInt("tab")];
        tab=view.getArguments().getString("tabvalue");
        adapter = new WeatherListAdapter(getView().getContext());

        getView().recyclerView.setLayoutManager(new LinearLayoutManager(getView().getContext()));
        getView().recyclerView.setAdapterWithProgress(adapter);
        //添加轮播图
//        adapter.addHeader(new RollViewPagerItemView(getView().recyclerView.getSwipeToRefresh(), MainFragment.class));
        getView().recyclerView.setRefreshListener(this);
        onRefresh();
    }

    @Override
    public void onRefresh() {

        getDataByTab(tab);


    }

    private void getDataByTab(String tab) {
        new WeatherBeanModel().getWeatherInfoBeanList(tab, new Callback<WeatherInfoBean>() {
            @Override
            public void onResponse(Call<WeatherInfoBean> call, Response<WeatherInfoBean> response) {
                WeatherInfoBean weatherInfoBean = response.body();

                if(weatherInfoBean!=null&&weatherInfoBean.getError_code()==0){

                    adapter.clear();
                    adapter.add(weatherInfoBean.getResult().getData().getRealtime());
                    adapter.add(weatherInfoBean.getResult().getData().getLife());
                    //adapter.add(weatherInfoBean.getResult().getData().getWeather());
                    //  adapter.add(weatherInfoBean.getResult().getData().getPm25());

                }

            }

            @Override
            public void onFailure(Call<WeatherInfoBean> call, Throwable t) {
                Log.d(TAG, "onFailure: 网络错误"+t.toString());
                JUtils.Toast("网络不给力");
                getView().recyclerView.showError();
                getView().recyclerView.getSwipeToRefresh().setRefreshing(false);
            }
        });

    }


}
